using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using DTOLayer;

namespace DAOLayer
{
    public class TacGiaDAO : AbstractDao
    {
        public int TestExisted(TacGiaDTO dto)
        {
            int kq = -1;
            try
            {
                SqlConnection cn;
                cn = this.SqlConnectiondata();
                cn.Open();
                String str = "select count(*) from TacGia where tentacgia = '" + dto.TenTacGia + "' and ngaysinh = '" + dto.NgaySinh + "'";
                SqlCommand cmd = new SqlCommand(str, cn);
                if ((int)cmd.ExecuteScalar() == 0)
                    kq = 1;
                cn.Close();
                return kq;
            }
            catch (Exception e)
            {
                return kq;
            }
        }
        public int InsertTacGia(TacGiaDTO dto)
        {
           
            int kq = TestExisted(dto);
            if (kq == -1)
            {
                return -1;
            }
            try
            {
                SqlConnection cn;
                cn = this.SqlConnectiondata();
                cn.Open();
                String strSQL = " insert into TacGia values(";
                strSQL += dto.MaTacGia + ",'" + dto.TenTacGia+ "','" + dto.NgaySinh + "','" + dto.GioiTinh + "','" + dto.Quoctich + "')";
                SqlCommand cmd = new SqlCommand(strSQL, cn);
                cmd.ExecuteNonQuery();
                cn.Close();
                return dto.MaTacGia;
            }
            catch (Exception e)
            {
                return -1;
            }
        }
        public int DeleteTacGia(int matacgia)
        {
            try
            {
                SqlConnection cn;
                cn = this.SqlConnectiondata();
                cn.Open();
                String strSQL = " delete  TacGia where Matacgia = " + matacgia;
                SqlCommand cmd = new SqlCommand(strSQL, cn);
                cmd.ExecuteNonQuery();
                cn.Close();
                return 1;
            }
            catch (Exception e)
            {
                return -1;
            }

        }
        public int UpdateTacGia(TacGiaDTO dto)
        {
            try
            {
                SqlConnection cn;
                cn = this.SqlConnectiondata();
                cn.Open();
                String strSQL = " Update  TacGia set Tentacgia='" + dto.TenTacGia + "'and NgaySinh='" + dto.NgaySinh + "'and  GioiTinh='" + dto.GioiTinh+ "'and Quoctich='" + dto.Quoctich + "' where matacgia = " + dto.MaTacGia;
                SqlCommand cmd = new SqlCommand(strSQL, cn);
                cmd.ExecuteNonQuery();
                cn.Close();
                return 1;
            }
            catch (Exception e)
            {
                return -1;
            }

        }
        public DataTable LayDanhSach()
        {
            DataTable dt = new DataTable();
            try
            {
                SqlConnection cn;
                cn = this.SqlConnectiondata();
                cn.Open();
                String str = "select * from TacGia";

                SqlDataAdapter d = new SqlDataAdapter(str, cn);
                d.Fill(dt);
                cn.Close();

            }
            catch (Exception e)
            {
                return null;
            }
            return dt;
        }
    }
}
